package itsm.isperp.module.controller.configuration;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.web.controller.TreeNodeController;
import itsm.isperp.module.entity.configuration.ConfCategory;
import itsm.isperp.module.entity.configuration.ConfLocation;
import itsm.isperp.module.service.configuration.ConfLocationService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 配置分类管理控制器
 * 
 * @author lizx
 * @date 2013-4-30
 */
@Controller
@RequestMapping("configuration/location")
public class ConfLocationController extends TreeNodeController<ConfLocation> {

	@Autowired
	ConfLocationService ConfLocationService;

	@Override
	public ConfLocationService getService() {
		return ConfLocationService;
	}

	@Override
	public String getPageTitle() {
		return "配置位置";
	}

	@RequestMapping("getTree")
	@ResponseBody
	@Override
	public List<ConfLocation> getTree(String id, boolean asyncLoad,
			String parentId, boolean showRoot, HttpServletRequest req) {
		if (asyncLoad) {

			JdbcTemplate jdbcTemplate = ContextHolder
					.getSpringBean("jdbcTemplate");

			List<ConfLocation> list = this.getService().getChildren(id,
					showRoot);

			String sql = "SELECT count(*) FROM conf_ci a INNER JOIN conf_category b  ON a.conf_category_id = b.id WHERE     b.treenode_path LIKE ";

			if ("1".equals(req.getParameter("showNumber"))) { 	
				for (ConfLocation c : list) {
					int count = jdbcTemplate.queryForObject(
							sql + "'%/" + c.getId() + "/%'", Integer.class);

					c.setName(c.getName() + "(<label style='color:blue'>"
							+ count + "</label>)");
				}
			}
			return list;

		}

		return this.getService().getTree(parentId, showRoot);
	}

}
